#!/bin/bash

DOXYGEN=doxygen
DOXYGEN_LOCATION=unknown

find_doxygen() { 
    # Search in path
    type_output="`type -P $DOXYGEN`"
    if [ "$?" -eq "0" ]; then
        DOXYGEN_LOCATION="$type_output"
        return 0
    fi

    # Search for Doxygen.app in Applications
    local doxapploc="/Applications/Doxygen.app/Contents/Resources/$DOXYGEN"
    if [ -f $doxapploc ]
    then
        DOXYGEN_LOCATION=$doxapploc
        return 0
    fi

    return 1
}

show_error() {
    osascript -e "tell app \"Xcode\" to display dialog \"$1\" with icon stop buttons {\"OK\"} default button 1" >/dev/null 2>/dev/null &
}

find_doxygen
if [ "$?" -ne "0" ]; then
    echo "Could not find doxygen." >&2
    show_error "Doxygen was not found on this system.\n\nTo build the documentation you need to install doxygen. Please go to\n\nhttp://www.stack.nl/~dimitri/doxygen/download.html#latestsrc\n\nand download the binary distribution. Install it to your /Applications folder and try rebuilding.\n\nAn alternative solution is adding a binary of doxygen to your \$PATH."
    exit 1;
fi

echo "Doxygen is: $DOXYGEN_LOCATION"

$DOXYGEN_LOCATION
